package com.example.administrator.hqq.activity.drag;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.hqq.R;
import com.example.administrator.hqq.View.alertview.AlertView;
import com.example.administrator.hqq.base.MyActivity;
import com.example.administrator.hqq.config.LoongsConfig;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class PhotoActivity extends MyActivity {
    private static final int DELETE_PIC = 1;
    @BindView(R.id.back)
    ImageView mBack;
    @BindView(R.id.view_pager)
    ViewPager mViewPager;
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.iv_delete)
    ImageView ivDelete;
    public Context mContext;
    private List<String> mList;
    private boolean canEdit = false;
    private AlertView deletePicAlertView;
    private String mFromType;

    /**
     * @param pics  图片集合
     * @param index 当前定位图片index
     */
    public static void start(Context context, ArrayList<String> pics, int index) {
        start(context, pics, index, false, "");
    }

    /**
     * @param pics     图片集合
     * @param index    当前定位图片index
     * @param canEdit  是否可编辑
     * @param fromType 来源
     */
    public static void start(Context context, ArrayList<String> pics, int index, boolean canEdit, String fromType) {
        Intent intent = new Intent(context, PhotoActivity.class);
        intent.putExtra(LoongsConfig.data, pics);
        intent.putExtra(LoongsConfig.index, index);
        intent.putExtra(LoongsConfig.canEdit, canEdit);
        intent.putExtra(LoongsConfig.FROM_TYPE, fromType);
        context.startActivity(intent);

    }

    /**
     * @param pic 查看一张图片
     */
    public void start(Context context, String pic) {
        ArrayList<String> pics = new ArrayList<>();
        pics.add(pic);
        start(context, pics, 0);
    }

    /**
     * @param pic      图片集合
     * @param canEdit  是否可编辑
     * @param fromType 来源
     */
    public static void start(Context context, String pic, boolean canEdit, String fromType) {
        ArrayList<String> pics = new ArrayList<>();
        pics.add(pic);
        start(context, pics, 0, canEdit, fromType);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo1);
        ButterKnife.bind(this);
        mContext = PhotoActivity.this;
        initView();
        bindListener();

    }

    private void bindListener() {
        ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deletePicAlertView.show();
            }
        });
    }

    private void initView() {
        mList = getIntent().getStringArrayListExtra(LoongsConfig.data);
        int index = getIntent().getIntExtra(LoongsConfig.index, 0);
        canEdit = getIntent().getBooleanExtra(LoongsConfig.canEdit, false);
        mFromType = getIntent().getStringExtra(LoongsConfig.FROM_TYPE);
        if (canEdit) {
            ivDelete.setVisibility(View.VISIBLE);
            initDeletePhotoView();
        }
        mTitle.setVisibility(mList.size() == 1 ? View.GONE : View.VISIBLE);
        mViewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return new PhotoFragment(mList.get(position));
            }

            @Override
            public int getCount() {
                return mList.size();
            }
        });
        mViewPager.setCurrentItem(index);
        mTitle.setText((index + 1) + "/" + mList.size());
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTitle.setText((position + 1) + "/" + mList.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void initDeletePhotoView() {
//        deletePicAlertView = new AlertView(null, getString(R.string.to_delete_photo), getString(R.string.cancel)
//                , new String[]{getString(R.string.confirm)}, null, mContext
//                , AlertView.Style.Alert, new com.accp.chuanzhong.citylivingroom.view.alertview.OnItemClickListener() {
//            @Override
//            public void onItemClick(Object o, int position) {
//                if (position == -1) {
//                    if (null != deletePicAlertView && deletePicAlertView.isShowing())
//                        deletePicAlertView.dismiss();
//                } else if (position == 0) {
//                    if (null != deletePicAlertView && deletePicAlertView.isShowing())
//                        deletePicAlertView.dismiss();
//                    switch (mFromType) {
//                        case LoongsConfig.TourModuleCode.FROM_TOURGUIDE_OWNPIC:
//                            EventBus.getDefault().post(LoongsConfig.TourModuleCode.FROM_TOURGUIDE_OWNPIC);
//                            break;
//                        case LoongsConfig.TourModuleCode.FROM_TOURGUIDE_CER1:
//                            EventBus.getDefault().post(LoongsConfig.TourModuleCode.FROM_TOURGUIDE_CER1);
//                            break;
//                        case LoongsConfig.TourModuleCode.FROM_TOURGUIDE_CER2:
//                            EventBus.getDefault().post(LoongsConfig.TourModuleCode.FROM_TOURGUIDE_CER2);
//                            break;
//                        case LoongsConfig.TourModuleCode.FROM_TOURGUIDE_GUIDE1:
//                            EventBus.getDefault().post(LoongsConfig.TourModuleCode.FROM_TOURGUIDE_GUIDE1);
//                            break;
//                        case LoongsConfig.TourModuleCode.FROM_TOURGUIDE_GUIDE2:
//                            EventBus.getDefault().post(LoongsConfig.TourModuleCode.FROM_TOURGUIDE_GUIDE2);
//                            break;
//                        case LoongsConfig.TourModuleCode.mine_personal_details:
//                            EventBus.getDefault().post(LoongsConfig.TourModuleCode.mine_personal_details);//我的个人信息
//                            break;
//                        case LoongsConfig.TourModuleCode.FROM_CELEBRITY_PICS:
//                            EventBus.getDefault().post(LoongsConfig.TourModuleCode.FROM_CELEBRITY_PICS);//名流馆——图集
//                            break;
//                        default:
//                            EventBus.getDefault().post(LoongsConfig.DELETE_PIC);
//                            break;
//                    }
//                    finish();
//
//                }
//            }
//        });
    }

    public void setCanEdit(boolean canEdit) {
        this.canEdit = canEdit;
    }

    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    protected String getActivityName() {
        return this.getClass().getSimpleName();
    }
}
